<template>
    <div>
      <div>
        <el-dialog
          title="预览"
          :visible.sync="dialogPreview"
          width="35%"
          :before-close="handleClose"
        >
          <div id="player" class="player"></div>
          <!-- <span slot="footer" class="dialog-footer">
            <el-button @click="dialogPreview = false">取 消</el-button>
            <el-button type="primary" @click="dialogPreview = false"
              >确 定</el-button
            >
          </span> -->
        </el-dialog>
      </div>
    </div>
  </template>
    
    <script>
  export default {
    name: "previewDialog",
    data() {
      return {
        dialogPreview: false,
        liveSdk:null
      };
    },
    created(){
        let script2 = document.createElement("script");
    script2.type = "text/javascript";
    script2.src =
      "https://s1.videocc.net/library/blueimp-md5/md5-2.18.0.min.js";
    let script3 = document.createElement("script");
    script3.type = "text/javascript";
    script3.src = "https://player.polyv.net/livesdk/polyv-live.min.js";
    document.body.appendChild(script2);
    document.body.appendChild(script3);
    },
    watch:{
        dialogPreview(val){
          if(val==true){
        this.getVideo();
      }
      if (this.liveSdk) {
        this.liveSdk.destroy(true)
      }
        }
    },
    methods: {
      handleClose() {
       this.dialogPreview=false
      },
      getVideo() {
        this.liveSdk = null;
        //注意事项：appSecret，appId不建议在前端显示，不安全，请您一定要请求后端接口获取sign
        //参数开始
        var appSecret = "cfaea33980f341f993b0de9a164e8b7b";
        var appId = "fmro8dyh3d";
        var channelId = "3744789";
        //参数结束
        var userId =
          new Date().getTime().toString() +
          (10000 + parseInt(Math.random() * 90000));
        var userName = "测试-" + userId;
        var userPic =
          "https://livestatic.videocc.net/assets/wimages/missing_face.png";
        var params = {
          appId: appId,
          channelId: channelId,
          timestamp: new Date().getTime(),
        };
  
        var sign = this.getSign(params, appSecret);
        // 第三步：创建SDK实例
        this.$nextTick(() => {
          this.liveSdk = new PolyvLiveSdk({
            channelId: channelId,
            sign: sign, // 频道验证签名
            timestamp: params.timestamp, // 毫秒级时间戳
            appId: appId, // polyv 后台的appId
            user: {
              userId: userId,
              userName: userName,
              pic: userPic,
            },
          });
  
          // 第四步：监听频道信息读取完成事件，初始化播放器
          this.liveSdk.on(
            PolyvLiveSdk.EVENTS.CHANNEL_DATA_INIT,
            async (event, data) => {
              this.liveSdk.setupPlayer({
                pptEl: "#ppt",
                el: "#player",
                autoplay: true,
                type: "vod",
                fileId: "70b6e5e90da9826804d881f611917d01",
                url: "https://playback-hw.videocc.net/record/recordf/push-hw1.videocc.net/mp4/c70d7424f916758273860120bcb/2023-02-10-04-15-22.mp4",
                sessionId: " gi3cxe7o4g",
              });
            }
          );
        });
      },
      getSign(params, appSecret) {
        var paramsStr = "";
        for (let key in params) {
          paramsStr += key + "" + params[key];
        }
        var finalStr = appSecret + paramsStr + appSecret;
        return md5(finalStr).toUpperCase();
      },
    },
  };
  </script>
    
    <style lang="scss" scoped>
  .ppt,
  .player {
    width: 100%;
    height: 360px;
  }
  
  .btn-wrap {
    margin-top: 15px;
  }
  </style>